def find_best_epoch(logfile):
    with open(logfile, 'r') as f:
        logs = f.read().strip().split('\n\n')[-1].strip().split('\n')
    malign_recalls = []
    epoches = []
    for log in logs:
        loglst = log.split()
        assert loglst[1] == 'epoch'
        epoches.append(loglst[2])
        assert loglst[-8] == 'val_malign_recalls'
        malign_recalls.append([float(r) for r in loglst[-3:-8:-1]])
    maxi = max(zip(malign_recalls, epoches))
    print(maxi)
    return maxi[-1]


if __name__ == '__main__':
    # print(find_best_epoch('/ssd_1t/huzq/kaggle_log/cddv9.log'))
    # for i in range(1, 5):
    #     print(find_best_epoch('/ssd_1t/huzq/kaggle_log/cddv9_valid{i}.log'.format(i=i)))
    #
    # print(find_best_epoch('/ssd_1t/huzq/kaggle_log/cddv9_dynamic_valid0.log'))
    #
    # for i in range(0, 5):
    #     print(find_best_epoch('/ssd_1t/huzq/kaggle_log/cddv9_vgg13shortcutsv2_bs16_valid{i}.log'.format(i=i)))

    for i in range(4):
        find_best_epoch('/ssd_1t/huzq/kaggle_log/cddv15_v2_valid{i}.log'.format(i=i))
        find_best_epoch('/ssd_1t/huzq/kaggle_log/cddv15_vgg13shortcutsv2_valid{i}.log'.format(i=i))
